import Vue from 'vue'
import VueRouter from 'vue-router'
import {Message} from "element-ui";
import Login from "@/views/user/Login";

import Register from "@/views/user/Register";

import AnimalCard from "@/components/animal/AnimalCard";

import Test from "@/views/Test";

import Index from "@/layout/Index.vue";
import Home from "@/views/Index/Home.vue";
import SchoolHome from "@/views/school/SchoolHome.vue";
import AnimailHome from "@/views/animal/AnimailHome.vue";
import AboutTeam from "@/views/team/AboutTeam.vue";
import Code from "@/views/team/Code.vue";
import AnimalInfo from "@/views/animal/AnimalInfo.vue";
import TeamInfo from "@/views/team/TeamInfo.vue";
import BgIndex from '@/views/bg/index.vue';
import UserInfo from "@/views/bg/info/UserInfo.vue";
import {getToken} from "@/util/TokenUtil";
import MyArticle from "@/views/bg/article/MyArticle.vue";
import EditArticle from "@/views/bg/article/EditArticle.vue";
import Article from "@/views/article/Article.vue";
import FourZeroFourError from '@/views/error/404Error.vue';
import MyType from "@/views/bg/article/MyType.vue";
import CollectArticle from "@/views/bg/article/CollectArticle.vue";
// 解决重复访问同一个页面时的报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: "/index/home",
        component: Index
    },
    {
        path: '/index',
        redirect: "/index/home",
        component: Index,
        children: [
            {
                path: "home",
                component: Home
            },
            {
                path: "article",
                component: Article
            },
            {
                path: "school",
                component: SchoolHome
            },
            {
                path: "animal",
                component: AnimailHome,
            },
            {
                path: "animal/info/:id",
                component: AnimalInfo
      },
      {
        path:"about",
        component: AboutTeam,
        children:[
          {
            path:"code",
            component:Code
          },
            {
                path: "team",
                component: TeamInfo
            }
        ]
      }
        ]
    },
    {
        path: '/bg',
        component: BgIndex,
        meta: {
            check: true,

        },
        children: [
            {
                path: "user/info",
                component: UserInfo,
                meta: {
                    check: true
                }
            },
            {
                path: 'article/info',
                component: MyArticle,
                meta: {
                    check: true
                }
            },
            {
                path: 'article/edit',
                component: EditArticle,
            },
            {
                path: 'article/type',
                component: MyType,
            },
            {
                path: 'article/collect',
                component: CollectArticle,
            }
        ]
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/register',
        component: Register,
        meta: {
            title: '注册',
            check: false
        }
    },

    {
        path: '/card',
        component: AnimalCard
    },
    {
        name: "mobile",
        path: '/mobile',
        component: Test
    },
    {
        path: "/test",
        component: Test
    },
    {
        path: '/404',
        component: FourZeroFourError
    },
]

const router = new VueRouter({
  mode: 'history',   // 模式，默认hash
  routes
})

router.beforeEach((to,from,next)=> {
    // 检查是否存在404

    if (to.matched.length === 0) {
        next("/404")
    }
    // 检查token
    if (to.meta.check) {
        let token = getToken();
        if (token) {
            // 登录了
            next()
        } else {
            // 未登录
            Message.error("请先登录")
            next("/login")
        }
    }
    next()
})

router.afterEach((to,from)=>{
  if(to.meta.title){
    document.title=to.meta.title
  }
  window.scroll(0,0)

})

export default router
